package org.nzj.useradmin.dao.impl;

/*
 * 2012-5-18 webproject update ssh2 version1.0
 * @author <a href="mailto:nzj1981@gmail.com">nzj</a>
 */
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.nzj.useradmin.dao.IUserDAO;
import org.nzj.useradmin.vo.User;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public class IUserDAOImpl implements IUserDAO {
	// 装配sessionFactory
	@Resource
	private SessionFactory sessionFactory;
	// @Resource private JdbcTemplate jTemplate;
	int count = 0;

	public boolean doCreate(User user) throws Exception {
		boolean flag = false;
		if (!this.doIf(user.getUserno())) {
			sessionFactory.getCurrentSession().persist(user);
			flag = true;
		} else {
			flag = false;
		}

		/*
		 * 利用sessionFactory保存数据无返回值,用jdbcTemplate可以返回值
		 */
		/*
		 * try { jTemplate.execute(new ConnectionCallback() {
		 * 
		 * public Object doInConnection(Connection con) throws SQLException,
		 * DataAccessException { Stringsql=
		 * "insert into t_user(id,userno,name,passwd) values(seq_t_user_id.nextval,?,?,?)"
		 * ; PreparedStatement ps=con.prepareStatement(sql); ps.setString(1,
		 * user.getUserno()); ps.setString(2, user.getName()); ps.setString(3,
		 * user.getPasswd()); ps.executeUpdate(); return null; } }); flag=true;
		 * } catch (Exception e) { flag=false; }
		 */

		return flag;

	}

	public boolean doDelete(int id) throws Exception {
		boolean flag = false;
		try {
			sessionFactory.getCurrentSession().delete(
					sessionFactory.getCurrentSession().load(User.class, id));
			flag = true;
		} catch (RuntimeException e) {
			flag = false;
		}
		return flag;
	}

	public boolean doIf(String keyWord) throws Exception {
		String sql = "select user.userno from User user where user.userno='"
				+ keyWord + "'";
		return sessionFactory.getCurrentSession().createQuery(sql).iterate()
				.hasNext();
	}

	public boolean doUpdate(User user) throws Exception {
		try {
			String sql = "update User set userno=:userno,name=:name,passwd=:passwd,sex=:sex,birthday=:birthday,email=:email,pic=:pic,ip=:ip where id=:id";
			Query query = sessionFactory.getCurrentSession().createQuery(sql);
			query.setString("userno", user.getUserno());
			query.setParameter("name", user.getName());
			query.setString("passwd", user.getPasswd());
			query.setString("sex", user.getSex());
			query.setDate("birthday", user.getBirthday());
			query.setString("email", user.getEmail());
			query.setString("pic", user.getPic());
			query.setString("ip", user.getIp());
			query.setParameter("id", user.getId());
			if (query.executeUpdate() > 0) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/*
	 * @function 表示数据库的返回一组对象查询操作
	 * 
	 * @param keyWord 关键字
	 * 
	 * @param curPage 当前页码
	 * 
	 * @param pageSize 每页显示记录数
	 * 
	 * @return 返回list集合
	 */
	@SuppressWarnings("unchecked")
	public List<User> findAll(String keyWord, int curPage, int pageSize)
			throws Exception {
		List<User> list = null;
		String sql = "from User u where u.name like :keyWord or u.userno like :keyWord "
				+ "or u.sex like :keyWord or to_char(u.birthday,'yyyymmdd') like :keyWord or u.ip like :keyWord order by id";
		try {
			Query query = sessionFactory.getCurrentSession().createQuery(sql);
			query.setString("keyWord", "%" + keyWord + "%");
			if (curPage != 0 && pageSize != 0) {
				query.setFirstResult((curPage - 1) * pageSize);
				query.setMaxResults(pageSize);
			}
			list = query.list();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/*
	 * @function 根据id返回一组对象
	 * 
	 * @return 无此id返回null
	 */
	public User findById(int id) throws Exception {
		String sql = "from User where id=" + id;
		return (User) sessionFactory.getCurrentSession().createQuery(sql)
				.uniqueResult();
	}

	/*
	 * @function 根据userNO返回一组对象
	 * 
	 * @return 无此userNO返回null
	 */
	public User findByUserNO(String userNO) throws Exception {
		String sql = "from User where userno='" + userNO + "'";
		return (User) sessionFactory.getCurrentSession().createQuery(sql)
				.uniqueResult();
	}

	/*
	 * @function 核查用户登陆
	 * 
	 * @return 如果用户存在返回true,并返回用户姓名
	 */
	@SuppressWarnings("unchecked")
	public boolean findLogin(User user) throws Exception {
		boolean flag = false;
		String sql = "from User where userno=:userno and passwd=:passwd";
		Query query = sessionFactory.getCurrentSession().createQuery(sql);
		query.setParameter("userno", user.getUserno());
		query.setParameter("passwd", user.getPasswd());
		Iterator<User> iter = query.list().iterator();
		if (iter.hasNext()) {
			flag = true;
			user.setName(iter.next().getName());
		} else {
			flag = false;
		}

		return flag;
	}
	/*
	 * @function 根据关键字查询返回总数
	 * @return 返回总数
	 * */
	public int getAllCount(String keyWord) throws Exception {
		String sql = "from User u where u.name like :keyWord or u.userno like :keyWord "
				+ "or u.sex like :keyWord or to_char(u.birthday,'yyyymmdd') like :keyWord or u.ip like :keyWord";
		try {
			Query query = sessionFactory.getCurrentSession().createQuery(sql);
			query.setString("keyWord", "%" + keyWord + "%");
			count = query.list().size();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}

}
